package com.onetech.adrewards.free.apicall;

import jp.co.CAReward_Ack.CARController;
import jp.co.CAReward_Common.CARCheck;
import jp.co.CAReward_Media.CARMWebViewClient;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class ZuZuCARMWebView extends WebView  {   

	/* バージョン */
	private static final String version = "6.0.0";
	/** ログ出力タグ */
	private static final String LOGTAG = "CarmWebView->";
	/** WebViewClient */
	private ZuZuCARMWebViewClient mCarmWebViewClient = null;
	
	/**
	 * コンストラクタです。(Context object)<br>
	 * @param context Context
	 *
	 */
	public ZuZuCARMWebView (Context context) {
		super(context);
	}

	public ZuZuCARMWebView (Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@SuppressLint("SetJavaScriptEnabled")
	public void init(Activity activity, String baseurl, String loading_msg, String get_m_id, 
			String get_m_owner_id, String get_user_id, String get_platform_id, 
			String get_api_key, String get_app_key, String get_timeout, boolean use_point_manage,View progressBar) {

		// バージョン表示
		Log.d(LOGTAG, "CARWebView:Ver " + version);
		
		// Root化されていないかのチェック
		CARCheck check = new CARCheck(activity.getApplicationContext());

		if (check.isSuperUserCustomized()) {
			Log.d(LOGTAG,"super user customized");
			return;
		}
		if (use_point_manage == true){
			if (CARController.upm_prms._onetime_key == null){
				Log.d(LOGTAG,"nothing onetime key... ");
				return;
			}
		}
		String url  = baseurl;
		mCarmWebViewClient = new ZuZuCARMWebViewClient(activity, loading_msg, 
				get_m_id, get_m_owner_id, get_user_id, get_platform_id, 
				get_api_key, get_app_key, get_timeout,progressBar);
		if (use_point_manage == true){
			mCarmWebViewClient.usePointManage();
		}
		setWebViewClient(mCarmWebViewClient);

		getSettings().setBuiltInZoomControls(true);
		getSettings().setSupportZoom(true);

		WebSettings settings = getSettings();
		settings.setJavaScriptEnabled(true);
		if (use_point_manage == true){
	        setWebChromeClient(new WebChromeClient(){
	        	@Override
	        	public boolean onJsAlert(WebView view, String url, String message, android.webkit.JsResult result){
	        		return super.onJsAlert(view, url, message, result);
	        	}
	        });
		}
		settings.setCacheMode(WebSettings.LOAD_NO_CACHE); 
		loadUrl(url);
	}

	public void dismissDialog() {
    	if (mCarmWebViewClient != null) {
	    	try {
	    		mCarmWebViewClient.dismissDialog();
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    	}
		}
	} 

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {	
    	if ((keyCode == KeyEvent.KEYCODE_BACK) && (this.canGoBack())) {
    		this.goBack();
    		return true;
    	}
    	return super.onKeyDown(keyCode, event);
	}
}